package com.mercycrazy.recruit.util;

import java.security.SecureRandom;

/**
 * 随机工具类
 *
 * @author <a href="mailto:1443424326@qq.com">mercycrazy</a>
 * @since 2023-01-14
 */
public class RandomUtil {

    private static final String ALL_CHAR_NUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    /**
     * 获取验证码随机数
     *
     * @param length 验证码长度
     * @return 验证码
     */
    public static String getRandomCode(int length) {

        String sources = "0123456789";
        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; ++i) {
            sb.append(sources.charAt(random.nextInt(10)));
        }
        return sb.toString();
    }

    /**
     * 生成指定长度随机字母和数字
     *
     * @param length 随机串长度
     * @return 随机串
     */
    public static String getStringNumRandom(int length) {
        // 生成随机数字和字母
        SecureRandom random = new SecureRandom();
        StringBuilder saltString = new StringBuilder(length);
        for (int i = 1; i <= length; ++i) {
            saltString.append(ALL_CHAR_NUM.charAt(random.nextInt(ALL_CHAR_NUM.length())));
        }
        return saltString.toString();
    }
}

